
using System;
using System.Data;
using System.Linq;
using System.Linq.Dynamic;
using System.Linq.Dynamic.Core;
using System.Linq.Expressions;
using System.Threading.Tasks;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;

using Abp.UI;
using Abp.AutoMapper;
using Abp.Extensions;
using Abp.Authorization;
using Abp.Domain.Repositories;
using Abp.Application.Services.Dto;
using Abp.Linq.Extensions;


using Yozeev.SystemConfig.SystemRecord;
using Yozeev.SystemConfig.SystemRecord.Dtos;
using Yozeev.SystemConfig.SystemRecord.DomainService;
using Yozeev.SystemConfig.SystemRecord.Authorization;
using Yozeev.Common;
using Abp.Authorization.Users;

namespace Yozeev.SystemConfig.SystemRecord
{
    /// <summary>
    /// CheckRecord应用层服务的接口实现方法  
    ///</summary>
    [AbpAuthorize]
    public class CheckRecordAppService : YozeevAppServiceBase, ICheckRecordAppService
    {
        private readonly IRepository<CheckRecord, Guid> _entityRepository;

        private readonly ICheckRecordManager _entityManager;

		private readonly IQueryHelper _queryHelper;

		/// <summary>
		/// 构造函数 
		///</summary>
		public CheckRecordAppService(
        IRepository<CheckRecord, Guid> entityRepository
        ,ICheckRecordManager entityManager
			, IQueryHelper queryHelper
		)
        {
            _entityRepository = entityRepository; 
             _entityManager=entityManager;
			_queryHelper = queryHelper;
		}


        /// <summary>
        /// 获取CheckRecord的分页列表信息
        ///</summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PagedResultDto<CheckRecordListDto>> GetPaged(GetCheckRecordsInput input)
		{
			var expression = _queryHelper.WhereExp<CheckRecord>(input.queryData);
			var query = _entityRepository.GetAll().Where(expression);
			// TODO:根据传入的参数添加过滤条件
            

			var count = await query.CountAsync();

			var entityList = await query
					.OrderBy(input.Sorting).AsNoTracking()
					.PageBy(input)
					.ToListAsync();

			// var entityListDtos = ObjectMapper.Map<List<CheckRecordListDto>>(entityList);
			var entityListDtos =entityList.MapTo<List<CheckRecordListDto>>();

			return new PagedResultDto<CheckRecordListDto>(count,entityListDtos);
		}


		/// <summary>
		/// 通过指定id获取CheckRecordListDto信息
		/// </summary>
		public async Task<CheckRecordListDto> GetById(EntityDto<Guid> input)
		{
			var entity = await _entityRepository.GetAsync(input.Id);

		    return entity.MapTo<CheckRecordListDto>();
		}

		/// <summary>
		/// 获取编辑 CheckRecord
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		public async Task<GetCheckRecordForEditOutput> GetForEdit(NullableIdDto<Guid> input)
		{
			var output = new GetCheckRecordForEditOutput();
CheckRecordEditDto editDto;

			if (input.Id.HasValue)
			{
				var entity = await _entityRepository.GetAsync(input.Id.Value);

				editDto = entity.MapTo<CheckRecordEditDto>();

				//checkRecordEditDto = ObjectMapper.Map<List<checkRecordEditDto>>(entity);
			}
			else
			{
				editDto = new CheckRecordEditDto();
			}

			output.CheckRecord = editDto;
			return output;
		}


		/// <summary>
		/// 添加或者修改CheckRecord的公共方法
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		public async Task CreateOrUpdate(CreateOrUpdateCheckRecordInput input)
		{

			if (input.CheckRecord.Id.HasValue)
			{
				await Update(input.CheckRecord);
			}
			else
			{
				await Create(input.CheckRecord);
			}
		}


		/// <summary>
		/// 新增CheckRecord
		/// </summary>
		protected virtual async Task<CheckRecordEditDto> Create(CheckRecordEditDto input)
		{
			//TODO:新增前的逻辑判断，是否允许新增

            // var entity = ObjectMapper.Map <CheckRecord>(input);
            var entity=input.MapTo<CheckRecord>();
			

			entity = await _entityRepository.InsertAsync(entity);
			return entity.MapTo<CheckRecordEditDto>();
		}

		/// <summary>
		/// 编辑CheckRecord
		/// </summary>
		protected virtual async Task Update(CheckRecordEditDto input)
		{
			//TODO:更新前的逻辑判断，是否允许更新

			var entity = await _entityRepository.GetAsync(input.Id.Value);
			input.MapTo(entity);

			// ObjectMapper.Map(input, entity);
		    await _entityRepository.UpdateAsync(entity);
		}



		/// <summary>
		/// 删除CheckRecord信息的方法
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		public async Task Delete(EntityDto<Guid> input)
		{
			//TODO:删除前的逻辑判断，是否允许删除
			await _entityRepository.DeleteAsync(input.Id);
		}



		/// <summary>
		/// 批量删除CheckRecord的方法
		/// </summary>
		public async Task BatchDelete(List<Guid> input)
		{
			// TODO:批量删除前的逻辑判断，是否允许删除
			await _entityRepository.DeleteAsync(s => input.Contains(s.Id));
		}


		/// <summary>
		/// 导出CheckRecord为excel表,等待开发。
		/// </summary>
		/// <returns></returns>
		//public async Task<FileDto> GetToExcel()
		//{
		//	var users = await UserManager.Users.ToListAsync();
		//	var userListDtos = ObjectMapper.Map<List<UserListDto>>(users);
		//	await FillRoleNames(userListDtos);
		//	return _userListExcelExporter.ExportToFile(userListDtos);
		//}

    }
}


